import { createRouter, createWebHistory } from 'vue-router'
import {Toast} from 'vant'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {     // 重定向  最上方
      path: '/',
      redirect: 'home',
    },
    {
      path: '/home',
      name: 'home',
      meta:{
        isshownav:true
      },
      component: () => import('@/views/HomeView.vue') 
    },
    {
      path: '/cart',
      name: 'cart',
      meta:{
        isshownav:false
      },
      component: () => import('@/views/CartView.vue')
    },
    {
      path: '/login',
      name: 'login',
      meta:{
        isshownav:true
      },
      component: () => import('@/views/LoginView.vue')
    },
    {
      path: '/user',
      name: 'user',
      meta:{
        isshownav:true
      },
      component: () => import('@/views/UserView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegistView.vue'),
      children:[
        {
          path:'step1',
          name:'step1',
          component: () => import('@/components/register/RegistStep1.vue'),
        },
        {
          path:'step2',
          name:'step2',
          component: () => import('@/components/register/RegistStep2.vue'),
        },
        {
          path:'step3',
          name:'step3',
          component: () => import('@/components/register/RegistStep3.vue'),
        }
      ]
    },
    {
      path: '/city',
      name: 'city',
      component: () => import('@/views/CityView.vue')
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/SearchView.vue')
    },
    {
      path: '/details/:id?',
      name: 'details',
      component: () => import('@/views/DetailsView.vue')
    },
    {
      path: '/address',
      name: 'address',
      component: () => import('@/views/AddressView.vue')
    },
    {
      path: '/order',
      name: 'order',
      component: () => import('@/views/OrderView.vue')
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/views/NotfindView.vue') },

  ]
})

// 设置全局前置守卫--登录状态校验
router.beforeEach((to,from)=>{
  if(to.path=='/cart'){
    if(!localStorage.getItem('token')){
      Toast('请先登录哦!!')
      return {path:'/login'}
    }
  }
})

export default router
